package com.aye.formatconversion.util;

import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.CannotWriteException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.FieldKey;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class NcmToMp3Converter {
    static Cipher cipher;

    static {
        try {
            cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        }
    }


    public static void main(String[] args) {
        String ncmFilePath = "path/to/your/ncm/file.ncm";
        String mp3FilePath = "path/to/save/mp3/file.mp3";

        try {
            byte[] ncmFileBytes = Files.readAllBytes(Path.of(ncmFilePath));
            byte[] mp3Bytes = decryptNcmFile(ncmFileBytes);
            saveMp3File(mp3Bytes, mp3FilePath);
//            updateMp3Metadata(mp3FilePath);
            System.out.println("NCM to MP3 conversion completed successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static byte[] decryptNcmFile(byte[] ncmFileBytes) {
        try {
            byte[] keyBytes = Arrays.copyOfRange(ncmFileBytes, 22, 54);
            byte[] ivBytes = Arrays.copyOfRange(ncmFileBytes, 54, 86);
            byte[] encryptedBytes = Arrays.copyOfRange(ncmFileBytes, 86, ncmFileBytes.length);

            SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(ivBytes));

            byte[] decryptedBytes;
            try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                 CipherInputStream inputStream = new CipherInputStream(
                         Files.newInputStream(Path.of("path/to/your/ncm/file.ncm")), cipher)) {
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
                decryptedBytes = outputStream.toByteArray();
            }

            return decryptedBytes;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static void saveMp3File(byte[] mp3Bytes, String mp3FilePath) throws IOException {

    }
}
